package coding.song;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @创建人 xfliu
 * @创建时间 2021/9/15
 * @描述 歌曲的清单
 */
public class SongList extends SongManager implements Serializable {
    private final ArrayList<Song> songList = new ArrayList<>();//歌曲的清单
    @Override
    public boolean addSong(Song song) {
        if (songList.contains(song)) {

            return false;
        } else {
            songList.add(song);

            return true;
        }
    }

    @Override
    /**
     *@描述 删除歌曲
     *@参数 [java.lang.String]
     *@返回值 void
     *@创建时间 2021/9/15
     */
    public boolean delSong(String name) {
        Song song = serchSong(name);
        if (song == null) {

            return false;
        } else {
            this.songList.remove(song);

            return true;
        }
    }

    public ArrayList<Song> getSongList() {
        return songList;
    }

    @Override
    public boolean updateSong(String name, String oldName) {
        Song song = serchSong(oldName);
        if (song == null) {
            System.out.println(oldName + "不存在哦~");
            return false;
        } else {
            song.setName(name);
            System.out.println(oldName + "改名为：" + name);
            return true;
        }
    }
    @Override
    public void querySong() {
        System.out.println("===歌曲清单===");
        System.out.println("目前歌曲数目：" + songList.size());
        Iterator songs = songList.iterator();
        while (songs.hasNext()) {
            System.out.println(songs.next());
        }
    }

    @Override
    public Song serchSong(String name) {
        /**
         *@描述 查找歌曲
         *@参数 [java.lang.String]
         *@返回值 void
         *@创建时间 2021/9/15
         */
        Song song = null;
        for (Song nextSong : songList) {
            if (nextSong.getName().equals(name)) {
                song = nextSong;
                System.out.println(name + " 在歌曲列表中~");
                break;
            }
        }
        return song;
    }
}
